package idv.loveloser.dao;

import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * MdlAssignment entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see idv.loveloser.dao.MdlAssignment
 * @author MyEclipse Persistence Tools
 */

public class MdlAssignmentDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory
			.getLogger(MdlAssignmentDAO.class);
	// property constants
	public static final String COURSE = "course";
	public static final String NAME = "name";
	public static final String INTRO = "intro";
	public static final String INTROFORMAT = "introformat";
	public static final String ASSIGNMENTTYPE = "assignmenttype";
	public static final String RESUBMIT = "resubmit";
	public static final String PREVENTLATE = "preventlate";
	public static final String EMAILTEACHERS = "emailteachers";
	public static final String VAR1 = "var1";
	public static final String VAR2 = "var2";
	public static final String VAR3 = "var3";
	public static final String VAR4 = "var4";
	public static final String VAR5 = "var5";
	public static final String MAXBYTES = "maxbytes";
	public static final String TIMEDUE = "timedue";
	public static final String TIMEAVAILABLE = "timeavailable";
	public static final String GRADE = "grade";
	public static final String TIMEMODIFIED = "timemodified";

	public void save(MdlAssignment transientInstance) {
		Transaction trx =getSession().beginTransaction();
		log.debug("saving MdlAssignment instance");
		try {
			getSession().save(transientInstance);
			trx.commit();
			trx=null;
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			trx=null;
			throw re;
		}
	}

	public void delete(MdlAssignment persistentInstance) {
		log.debug("deleting MdlAssignment instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public MdlAssignment findById(java.lang.Long id) {
		log.debug("getting MdlAssignment instance with id: " + id);
		try {
			MdlAssignment instance = (MdlAssignment) getSession().get(
					"idv.loveloser.dao.MdlAssignment", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(MdlAssignment instance) {
		log.debug("finding MdlAssignment instance by example");
		try {
			List results = getSession()
					.createCriteria("idv.loveloser.dao.MdlAssignment")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding MdlAssignment instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from MdlAssignment as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCourse(Object course) {
		return findByProperty(COURSE, course);
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByIntro(Object intro) {
		return findByProperty(INTRO, intro);
	}

	public List findByIntroformat(Object introformat) {
		return findByProperty(INTROFORMAT, introformat);
	}

	public List findByAssignmenttype(Object assignmenttype) {
		return findByProperty(ASSIGNMENTTYPE, assignmenttype);
	}

	public List findByResubmit(Object resubmit) {
		return findByProperty(RESUBMIT, resubmit);
	}

	public List findByPreventlate(Object preventlate) {
		return findByProperty(PREVENTLATE, preventlate);
	}

	public List findByEmailteachers(Object emailteachers) {
		return findByProperty(EMAILTEACHERS, emailteachers);
	}

	public List findByVar1(Object var1) {
		return findByProperty(VAR1, var1);
	}

	public List findByVar2(Object var2) {
		return findByProperty(VAR2, var2);
	}

	public List findByVar3(Object var3) {
		return findByProperty(VAR3, var3);
	}

	public List findByVar4(Object var4) {
		return findByProperty(VAR4, var4);
	}

	public List findByVar5(Object var5) {
		return findByProperty(VAR5, var5);
	}

	public List findByMaxbytes(Object maxbytes) {
		return findByProperty(MAXBYTES, maxbytes);
	}

	public List findByTimedue(Object timedue) {
		return findByProperty(TIMEDUE, timedue);
	}

	public List findByTimeavailable(Object timeavailable) {
		return findByProperty(TIMEAVAILABLE, timeavailable);
	}

	public List findByGrade(Object grade) {
		return findByProperty(GRADE, grade);
	}

	public List findByTimemodified(Object timemodified) {
		return findByProperty(TIMEMODIFIED, timemodified);
	}

	public List findAll() {
		log.debug("finding all MdlAssignment instances");
		try {
			String queryString = "from MdlAssignment";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public MdlAssignment merge(MdlAssignment detachedInstance) {
		Transaction trx =getSession().beginTransaction();
		log.debug("merging MdlAssignment instance");
		try {
			MdlAssignment result = (MdlAssignment) getSession().merge(
					detachedInstance);
			trx.commit();
			log.debug("merge successful");
			trx=null;
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			trx=null;
			throw re;
		}
	}

	public void attachDirty(MdlAssignment instance) {
		log.debug("attaching dirty MdlAssignment instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(MdlAssignment instance) {
		log.debug("attaching clean MdlAssignment instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}